var $eachRate, $eachFeeNum, $eachMoney;
var OrderInfoObject;


// 跳到第二步
function _toFillUserInfo() {
    $("#countBox").hide();
    $("#cardInfo").show();
    $(".circle:eq(1)").addClass("checked");
}

// 跳到支付结果
function toResult() {
    $("#cardInfo").hide();
    $("#resultBox").show();
    $(".circle:eq(2)").addClass("checked");
}

// input 获得焦点
function infoFocusFuc() {
    var $theFormat = $(this).next("div.input-format");
    $theFormat.show();
}

// input 失去焦点隐藏提示
function infoInputBlur() {
    var $theFormat = $(this).next("div.input-format");
    $theFormat.hide();
}

// creditCardFormat 输入实时格式化提示
function creditCardFormat() {
    var thisVal = $(this).val();
    thisVal = thisVal.replace(/\D/g, "");
    $(this).val(thisVal);
    var $theFormat = $(this).next("div.input-format");
    if (thisVal.length < 4) {
        $theFormat.text(thisVal);
    } else if (thisVal.length > 4 && thisVal.length < 8) {
        $theFormat.text(thisVal.replace(/(\d{4})/, "$1  "));
    } else if (thisVal.length > 8 && thisVal.length < 12) {
        $theFormat.text(thisVal.replace(/(\d{4})(\d{4})/, "$1  $2  "));
    } else if (thisVal.length > 12) {
        $theFormat.text(thisVal.replace(/(\d{4})(\d{4})(\d{4})/, "$1  $2  $3  "));
    }

}

// 身份证格式提示
function userIdFormat() {
    var thisVal = $(this).val();
    thisVal = thisVal.replace(/[^0-9|X|x]/g, "");
    thisVal = thisVal.replace("x", "X");
    $(this).val(thisVal);
    var $theFormat = $(this).next("div.input-format");
    if (thisVal.length < 6) {
        $theFormat.text(thisVal);
    } else if (thisVal.length > 6 && thisVal.length < 14) {
        $theFormat.text(thisVal.replace(/(\d{6})/, "$1  "));
    } else if (thisVal.length > 14) {
        $theFormat.text(thisVal.replace(/(\d{6})(\d{8})/, "$1  $2  "));
    }
}

// 手机格式化提示
function userPhoneFormat() {
    var thisVal = $(this).val();
    thisVal = thisVal.replace(/\D/g, "");
    $(this).val(thisVal);
    var $theFormat = $(this).next("div.input-format");
    if (thisVal.length < 3) {
        $theFormat.text(thisVal);
    } else if (thisVal.length > 3 && thisVal.length < 7) {
        $theFormat.text(thisVal.replace(/(\d{3})/, "$1  "));
    } else if (thisVal.length > 8) {
        $theFormat.text(thisVal.replace(/(\d{3})(\d{4})/, "$1  $2  "));
    }
}

//  有效期格式提示
function dateFormat() {
    var thisVal = $(this).val();
    thisVal = thisVal.replace(/\D/g, "");
    $(this).val(thisVal);
    var $theFormat = $(this).next("div.input-format");
    if (thisVal == "") {
        $theFormat.text("04/18 请输入了0418");
    } else if (thisVal.length == 4) {
        $theFormat.text(thisVal.replace(/(\d{2})(\d{2})/, "20$2年$1月"));
    }
}

// 格式化cvn2
function numberFormat() {
    var thisVal = $(this).val();
    thisVal = thisVal.replace(/\D/g, "");
    $(this).val(thisVal);
}



// 初始化变量
function initView() {
    $eachRate = $("#eachRate");
    $eachFeeNum = $("#eachFeeNum");
    $eachMoney = $("#eachMoney");
    $("#cardInfo input").on("focus", infoFocusFuc);
    $("#cardInfo input").on("blur", infoInputBlur);
    $("#credit-card").on("input", creditCardFormat);
    $("#user-id").on("input", userIdFormat);
    $("#user-phone").on("input", userPhoneFormat);
    $("#time-num").on("input", dateFormat);
    $("#cvn2").on("input", numberFormat);
    $("#verify-code").on("input", numberFormat);
    $(".check_label").on("touchstart", function() {
        if ($("#agreeInput").attr("checked")) {
            $("#agreeInput").removeAttr("checked");
        } else {
            $("#agreeInput").attr("checked", true);
        }
        setAgreeDate()
    });
    setAgreeDate()
}

//  设置分期交易时间
function setAgreeDate() {
    if ($("#agreeInput").attr("checked")) {
        var theDate = new Date();
        var agreeMonth = theDate.getMonth()+1>9?theDate.getMonth()+1:'0'+(theDate.getMonth()+1)
        var agreeDate = theDate.getDate()
        $("#agreeYear").text(theDate.getFullYear());
        $("#agreeMonth").text(agreeMonth);
        $("#agreeDate").text(agreeDate);
    } else {
        $("#agreeYear").text(" ");
        $("#agreeMonth").text(" ");
        $("#agreeDate").text(" ");
    }
}

module.exports = {
    initView: initView,
    toResult: toResult,
    toFillUserInfo:_toFillUserInfo
};